package module13;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class hdfcbanklist {
public static WebDriver driver;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
      driver = new FirefoxDriver();
      driver.navigate().to("http://www.hdfcbank.com/");
      driver.findElement(By.xpath("//*[@id='hometab2']/div[2]/div[4]/div/p/a")).click();
      
      WebElement element = driver.findElement(By.tagName("a"));
      List<WebElement> lists = driver.findElements(By.tagName("a"));
      System.out.println(lists.size());
      
      String xP1= "//*[@id='quickapplydropdown']/li[";
      String xP2= "]";
      int i=1;
      String xP3;
      driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
      xP3= xP1+i+xP2;
      while(isElementPresent(xP1+i+xP2))
      {
    	  xP3= xP1+i+xP2;
    	  try
    	  {
    		  System.out.println("Text is:" + driver.findElement(By.xpath(xP3)).getText());
    		  System.out.println("Is displayed:" +driver.findElement(By.xpath(xP3)).isDisplayed());
    		  if(driver.findElement(By.xpath(xP3)).getText().equals("Credit Card"))
    		  {
    			  lists.get(i).click();
    			  Thread.sleep(5000);
    			  System.out.println(driver.getTitle());
    			  driver.navigate().to("http://www.hdfcbank.com/");
    			  
    		  }
    	  }
    	  catch(Throwable t)
    	  {
    		  System.out.println("Error");
    	  }
    	  i++;
      }
      /*for(i=1;i<lists.size();i++)
      {
    	 
      }*/
		      File screensht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
		      try {
				FileUtils.copyFile(screensht, new File("C:\\Manjsuha\\Selenium\\SeleniumQtpelearn\\hdfcbank.jpg"));
			} catch (Throwable t) {
				// TODO Auto-generated catch block
				t.printStackTrace();
			}
      driver.close();
	}
	public static boolean isElementPresent(String xP){
		 int count = driver.findElements(By.xpath(xP)).size();
		 if(count==0){
			 return false;
		 }
		 else{
			 return true;
		 }
	}

}
